package com.clubs.coding.Service.Impl;

import com.clubs.coding.Service.AthAdminCheckService;
import com.clubs.coding.exception.ImoocMallException;
import com.clubs.coding.exception.ImoocMallExceptionEnum;
import com.clubs.coding.model.dao.AthCheckMapper;
import com.clubs.coding.model.dao.AthRecordMapper;
import com.clubs.coding.model.dao.UserMapper;
import com.clubs.coding.model.pojo.AthCheck;
import com.clubs.coding.model.pojo.AthRecord;
import com.clubs.coding.model.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AthAdminCheckServiceImpl implements AthAdminCheckService {
@Autowired
    AthCheckMapper athCheckMapper;
@Autowired
    UserMapper userMapper;
@Autowired
    AthRecordMapper athRecordMapper;
@Override
 public PageInfo checklist(Integer pageNum, Integer pageSize,Integer type){
    PageHelper.startPage(pageNum, pageSize,"creat_time");

         List<AthCheck> athChecks = athCheckMapper.selectByupdate(type);
         if (athChecks.size()==0) {
             throw new ImoocMallException(ImoocMallExceptionEnum.CHECK_NULL);
         }
         PageInfo pageInfo = new PageInfo(athChecks);
         pageInfo.setList(athChecks);
         return pageInfo;




 }
 @Override
    public PageInfo checklistAll(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize,"creat_time");
        List<AthCheck> athChecks = athCheckMapper.selectByAll();
        if (athChecks.size()==0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.CHECK_NULL);
        }
        PageInfo pageInfo = new PageInfo(athChecks);
        pageInfo.setList(athChecks);
        return pageInfo;
    }
    @Transactional(rollbackFor = Exception.class)
@Override
    public void runcheck(Integer id){
        AthCheck athCheck = athCheckMapper.selectByPrimaryKey(id);
        if (athCheck==null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.CHECK_ID);
        }
        athCheck.setUpdateTime(new Date());
        athCheckMapper.updateByPrimaryKeySelective(athCheck);
        Integer userId = athCheck.getUserId();
        User user =userMapper.selectByPrimaryKey(userId);
        user.setIsMember(1);
        int i = userMapper.updateByPrimaryKeySelective(user);
        try {
            if (i==0) {
                throw new ImoocMallException(ImoocMallExceptionEnum.ALTER_NO);
            }
        }catch (ImoocMallException e){
            throw new ImoocMallException(ImoocMallExceptionEnum.ALTER_NO);
        }
        AthRecord athRecord = new AthRecord();
        athRecord.setUserId(userId);
        athRecord.setCreatTime(new Date());

        int i1 = athRecordMapper.insertSelective(athRecord);
        if (i1 == 0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.LEAVE_NULL);
        }
    }

}
